#include <iostream>
#include <stdio.h>
#include <map>
#include <algorithm>
using namespace std;
struct node {
	int Dia;
	string col;
	bool operator < (const node& a) const {
		return Dia<a.Dia;
	}
};
node res[25];
int StoI(string s) {
	int a=0;
	int len=s.length();
	for (int i=0; i<len; ++i) {
		a=a*10+s[i]-'0';
	}
	return a;
}
int main() {
	int N;
	string a;
	string b;
	while (cin>>N) {
		for (int i=0; i<N; ++i) {
			cin>>a>>b;
			if (a[0]<='9' && a[0]>='0') {
				res[i].Dia=StoI(a);
				res[i].col=b;
			}
			else {
				res[i].col=a;
				res[i].Dia=2*StoI(b);
			}
		}
		sort(res, res+N);
		for (int i=0; i<N; ++i) {
			cout<<res[i].col<<endl;
		}
	}
	return 0;
}
